package calao.contact;

import calao.client.MissingPermissionException;
import calao.contact.client.ContactService;
import calao.contact.client.Contact;
import calao.contact.client.Group;
import calao.storage.EqualCondition;
import calao.storage.Field;
import calao.storage.FieldCondition;
import calao.storage.ObjectSearch;
import calao.storage.Storage;
import calao.DesktopServiceImpl;

public class ContactServiceImpl 
extends DesktopServiceImpl 
implements ContactService 
{
	
	static class NotInOrgCondition extends FieldCondition
	{
		protected NotInOrgCondition() {
			super(ContactDB.PERSON_ORGANIZATION);
		}
		public boolean matches(Object value) {
			return value==null || ((Integer)value)==0;
		}
	}
	
	public static Field[] LEVEL1_FIELDS = {ContactDB.ID, ContactDB.NAME, ContactDB.TYPE,
		ContactDB.FIRST_NAME, ContactDB.GROUP, ContactDB.PERSON_ORGANIZATION};

	public static Field[] LEVEL2_FIELDS = {ContactDB.ID, ContactDB.NAME, ContactDB.TYPE,
		ContactDB.FIRST_NAME, ContactDB.GROUP, ContactDB.PERSON_ORGANIZATION,
		ContactDB.PHONE, ContactDB.FAX, ContactDB.EMAIL};

	public Contact getContactDetails(int cid) 
	{
		return getWorkspace().getStorage().fetchObject(ContactDB.TABLE, cid);
	}

	public Contact[] listContacts(int group, int org, int detailLevel) 
	{
		Storage db = getWorkspace().getStorage();
		ObjectSearch<Contact> search = ContactDB.TABLE.newObjectSearch();
		if (group!=-1) search.add(new EqualCondition(ContactDB.GROUP, group));
		if (org!=-1) search.add(new EqualCondition(ContactDB.PERSON_ORGANIZATION, org));
		if (group==-1 && org==-1) search.add(new NotInOrgCondition());
		if (detailLevel==1) search.limitTo(LEVEL1_FIELDS);
		else if (detailLevel==2) search.limitTo(LEVEL2_FIELDS);			
		return db.getResults(search);
	}

	public Group[] listGroups()
	{
		return getWorkspace().getStorage().getObjects(GroupDB.TABLE);
	}
	
	public Contact createContact(Contact details) 
	throws MissingPermissionException 
	{
		check(MODIFY_CONTACTS);
		Storage db = getWorkspace().getStorage();
		details.setId(db.getNextValue(ContactDB.ID_SEQUENCE));
		db.store(ContactDB.TABLE, details);
		return details;
	}

	public void save(Contact contact) 
	throws MissingPermissionException 
	{
		check(MODIFY_CONTACTS);
		Storage db = getWorkspace().getStorage();
		Contact old = db.fetchObject(ContactDB.TABLE, contact.getId());
		if (old==null) throw new IllegalArgumentException();
		if (old.getClass()!=contact.getClass()) throw new IllegalArgumentException();
		db.store(ContactDB.TABLE, contact);
	}

	public Group createGroup(Group details) 
	throws MissingPermissionException 
	{
		check(MODIFY_CONTACTS);
		Storage db = getWorkspace().getStorage();
		details.setId(db.getNextValue(GroupDB.ID_SEQUENCE));
		db.store(GroupDB.TABLE, details);
		return details;
	}

	public void save(Group group) 
	throws MissingPermissionException 
	{
		check(MODIFY_CONTACTS);
		Storage db = getWorkspace().getStorage();
		Group old = db.fetchObject(GroupDB.TABLE, group.getId());
		if (old==null) throw new IllegalArgumentException();
		db.store(GroupDB.TABLE, group);		
	}

}
